package com.zcx.qshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.qshop.R;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.adapter.SearchAdapter;
import com.zcx.qshop.conn.JsonHotwordsAsyGet;
import com.zcx.qshop.conn.JsonSerchlistAsyGet;
import com.zcx.qshop.db.QSDataBase;
import com.zcx.qshop.dialog.WhetherDialog;
import com.zcx.qshop.view.SearchBarView;
import com.zcx.qshop.view.TitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/8/23 0023.
 */
public class SearchActivity extends QSActivity {

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.search_search_bar_view) private SearchBarView searchBarView;

    @BoundView(R.id.search_grid_view) private GridView gridView;

    @BoundView(R.id.search_delete_layout) private View deleteLayout;

    @BoundView(R.id.search_delete_all) private View deleteAll;

    private List<JsonHotwordsAsyGet.Info.Word> hot = new ArrayList<>();

    private List<JsonHotwordsAsyGet.Info.Word> list = new ArrayList<>();

    private SearchBarView.OnItemClickListener onItemClickListener;

    private SearchAdapter adapter;

    private boolean isHot = true;

    private JsonSerchlistAsyGet jsonSerchlistAsyGet = new JsonSerchlistAsyGet("", QSApplication.QSPreferences.readCity().code, QSApplication.QSPreferences.readSchool().sid, new AsyCallBack<JsonSerchlistAsyGet.Info>(){

        @Override
        public void onSuccess(String toast, int type, JsonSerchlistAsyGet.Info info) throws Exception {

            QSDataBase.SearchTable.insert(jsonSerchlistAsyGet.keyword);

            if(!isHot){

                onItemClickListener.onAfterClick();

            }

            startVerifyActivity(SearchResultActivity.class, new Intent().putExtra("Info", info));

        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_search);

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.SEARCH_VIEW);

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

            @Override
            public void onSearchClick(String keyword) {

                jsonSerchlistAsyGet.keyword = keyword;

                jsonSerchlistAsyGet.execute(SearchActivity.this);

            }

        });

        gridView.setAdapter(adapter = new SearchAdapter(this, list) {

            @Override
            protected void onItemClick(JsonHotwordsAsyGet.Info.Word word) {

                titleView.setKeyword(jsonSerchlistAsyGet.keyword = word.title);

                jsonSerchlistAsyGet.execute(SearchActivity.this);

            }

        });

        searchBarView.setOnItemClickListener(onItemClickListener = new SearchBarView.OnItemClickListener() {

            @Override
            public void onHotClick() {

                isHot = true;

                list.clear();

                list.addAll(hot);

                adapter.notifyDataSetChanged();

                deleteLayout.setVisibility(View.GONE);

            }

            @Override
            public void onAfterClick() {

                isHot = false;

                list.clear();

                list.addAll(QSDataBase.SearchTable.queryAll());

                adapter.notifyDataSetChanged();

                deleteLayout.setVisibility(View.VISIBLE);

            }

        });

        deleteAll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                new WhetherDialog(SearchActivity.this, "清除历史数据") {

                    @Override
                    protected void onYes() {

                        QSDataBase.SearchTable.deleteAll();

                        onItemClickListener.onAfterClick();

                    }

                }.show();

            }

        });

        new JsonHotwordsAsyGet(new AsyCallBack<JsonHotwordsAsyGet.Info>(){

            @Override
            public void onSuccess(String toast, int type, JsonHotwordsAsyGet.Info info) throws Exception {

                hot.addAll(info.words);

                list.addAll(hot);

                adapter.notifyDataSetChanged();

            }

        }).execute(this);

    }

}
